import { ref, unref } from "vue";
import { useCacheStore } from "@/store/modules/cache"; // 自行用于缓存的内容
import { storeToRefs } from "pinia";
// 数据字典
declare interface DictItemFace{
   code:string
}
// 获取数据字典
interface DictDataFace extends Record<string, DictItemFace[]> {}
export const useDict = () => {
  const cacheStore = useCacheStore();
  const { dictList } = storeToRefs(cacheStore);
  const getDictList = <T extends string>(
    str: T[] | T
  ): Record<T, DictItemFace[] | undefined> => {
    let result: DictDataFace = {};
    // 兼容下传字符串的情况，当然，不建议这么做，会导致ts返回无约束
    if(typeof str==='string'){
      str =str.split(',') as string[]
    }
    for (let i = 0; i < str.length; i++) {
      const data = dictList?.value?.find((v) => v.code === str[i]);
      if (data) {
        result[str[i]] = data.dictItemDtoList;
      }
    }
    return result;
  };

  return {
    getDictList,
  };
};
